using System.Collections.Generic;
using AspNetMvc.Models;
using CoreVirtuoso.Config;
using Infrastructure.Events.Interfaces;
using VirtuosoDal.Entities;
namespace AspNetMvc.Helpers
{
    internal class EventForViewHelper
    {
        public static EventForView Build(IEventsOntologyClassesService eventsOntologyClassesService,
            Event @event,
            string language)
        {
            var eventForView = new EventForView(@event);

            eventForView.TypeLabel = eventsOntologyClassesService.GetClassLabel(EventsOntologyUris.EventTypeClassUri,
                                                            @event.Type, language);

            foreach (var category in @event.Categories)
            {
                eventForView.CategoriesLabels.Add(eventsOntologyClassesService.GetClassLabel(EventsOntologyUris.EventCategoryClassUri,
                                                                category, language));
            }



            return eventForView;
        }

        public static List<EventForView> Build(IEventsOntologyClassesService eventsOntologyClassesService,
            ResultSet<Event> resultSet,
            string language)
        {
            var eventsForView = new List<EventForView>();
            foreach (var @event in resultSet.Values)
            {
                eventsForView.Add(Build(eventsOntologyClassesService, @event, language));
            }
            return eventsForView;
        }

        public static void FillAdditionalProperties(EventForView eventForView,
            IEventsOntologyClassesService eventsOntologyClassesService,
            IEventPropertyDescriptionManager eventPropertyDescriptionManager,
            string language)
        {
            foreach (var property in eventForView.Event.AdditionalPropertiesWithValues.Keys)
            {
                var eventPropertyDescription = eventPropertyDescriptionManager.GetEventPropertyDescription(property, language);
                List<string> values = null;
                if (eventPropertyDescription.IsHierarchy)
                {
                    values = new List<string>();
                    foreach (var value in eventForView.Event.AdditionalPropertiesWithValues[property])    
                        values.Add(eventsOntologyClassesService.GetClassLabel(eventPropertyDescription.HierarchyRootUri, value, language));
                }
                else
                    values = eventForView.AdditionalPropertiesWithValues[eventPropertyDescription];
                eventForView.AdditionalPropertiesWithValues[eventPropertyDescription] = values;
            }
        }
    }
}